package com.tianqu.tms.core.base;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;

/**
 * 
 * @author liugang
 *
 */
public abstract class TmsEntity implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private boolean existing;
	
	public abstract void save() throws TmsDaoException,TmsException ;
	
	protected void setExisting(boolean b) {
		this.existing = b;
	}
	
	protected boolean isExisting() {
		return this.existing;
	}
	
	/**
	 * 
	 * @return
	 * @throws IrisException
	 */
	public TmsEntity copyThis() throws TmsException {
		
		ByteArrayOutputStream bos = new ByteArrayOutputStream();		
		ObjectOutputStream oos;
		try {
			oos = new ObjectOutputStream(bos);
			oos.writeObject(this);
			ObjectInputStream ois = new ObjectInputStream(
					   new ByteArrayInputStream(bos.toByteArray()));
			TmsEntity copyObject = (TmsEntity) ois.readObject();
			return copyObject;
		} catch (IOException e) {
			throw new TmsException("Clone Failed", e);
		} catch (ClassNotFoundException e) {
			throw new TmsException("Clone Failed", e);
		}
	}
	
}
